<?php

// Programmed by Reza Salehi - zaalion@yahoo.com - Free for non-commercial use. , Nov 2005
class dataGrid {

    var $dataSource;
    var $dataKeyField;
    var $nextPage;
    var $nextPageLable;
    var $colNum;
    var $fieldsToShow;
    var $rowClass;
    var $width;
    var $acciones;
    var $param;
    var $accionNum = 0;
    var $labelAcciones;
    var $fieldLabel; //array
    var $fieldFormat; //array

    //
    function setFieldLabel($arrayLabel) {
        $this->fieldLabel = $arrayLabel;
        
    }
    
    function setFieldFormat($fieldFormat){
        $this->fieldFormat = $fieldFormat;
    }        
    
    function addAccion($accion, $param) {
        $this->acciones[$this->accionNum] = $accion;
        $this->param[$this->accionNum] = $param;
        $this->accionNum++;
    }

    function setLabelAcciones($label) {
        $this->labelAcciones = $label;
    }

    function dataGrid($dataSource, $dataKeyField, $fieldsToShow, $nextPage, $nextPageLable, $rowClass, $width) {
        $this->dataSource = $dataSource;
        $this->dataKeyFeild = $dataKeyField;
        $this->nextPage = $nextPage;
        $this->nextPageLable = $nextPageLable;
        $this->fieldsToShow = $fieldsToShow;
        $this->colNum = sizeof($this->fieldsToShow);
        $this->rowClass = $rowClass;
        $this->width = $width;
    }

    //
    function create() {
        $rowClass = false;
        print("<table cellpadding=0 cellspacing=0 width=$this->width border=0>");
        print("<tr>");
        for ($i = 0; $i < $this->colNum; $i++) {
            print("<td>");
            print($this->fieldsToShow[$i]);
            print("</td>");
        }
        //
        if ($this->nextPage != NULL)
            print("<td>" . $this->nextPageLable . "</td>");
        //
        print("</tr>");
        //
        while ($row = mysql_fetch_array($this->dataSource)) {
            $rowClass = !$rowClass;
            if ($rowClass)
                print("<tr class=" . $this->rowClass . ">");
            else
                print("<tr>");
            for ($i = 0; $i < $this->colNum; $i++) {
                print("<td>");
                print($row[$this->fieldsToShow[$i]]);
                print("</td>");
            }
            //
            if ($this->nextPage != NULL)
                print("<td><a href=" . $this->nextPage . "?" . $this->dataKeyFeild . "=" . $row[$this->dataKeyFeild]
                        . " target=blank>" . $this->nextPageLable . "</a></td>");
            //
            print("</tr>");
        }
        print("</table>");
    }

    function header() {
        print("<tr>");
        if($this->fieldLabel != NULL && count($this->fieldLabel) > 0)
        {
            for ($i = 0; $i < count($this->fieldLabel); $i++) {
                print("<th>");
                print($this->fieldLabel[$i]);
                print("</th>");
            }
        }
        else
        {
            for ($i = 0; $i < $this->colNum; $i++) {
                print("<th>");
                print($this->fieldsToShow[$i]);
                print("</th>");
            }
        }
        //Titulo para proxima pagina
        if ($this->nextPage != NULL)
            print("<th>" . $this->nextPageLable . "</th>");
        //Titulo para acciones
        if ($this->accionNum > 0)
            print("<th>" . $this->labelAcciones . "</th>");

        print("</tr>");
    }
    
   

    function content() {
        $rowClass = false;

        while ($row = mysql_fetch_array($this->dataSource)) {
            $rowClass = !$rowClass;
            if ($rowClass) {
                print("<tr class=" . $this->rowClass . ">");
            } else {
                print("<tr>");
            }

            for ($i = 0; $i < $this->colNum; $i++) {
                print("<td>");
                print($this->formatLabel($this->fieldFormat[$i], $row[$this->fieldsToShow[$i]]));
                print("</td>");
            }
            //
            if ($this->nextPage != NULL) {
                print("<td><a href=" . $this->nextPage . "?" . $this->dataKeyFeild . "=" . $row[$this->dataKeyFeild] . " target=blank>" . $this->nextPageLable . "</a></td>");
            }

            if ($this->accionNum > 0) {
                print("<td>");
                for ($i = 0; $i < $this->accionNum; $i++) {
                    //valida el tipo de variable de param
                    if (is_array($this->param[$i])) {
                        for ($j = 0; $j < count($this->param[$i]); $j++) {
                            print(str_replace('%' . $this->param[$i][$j] . '%', $row[$this->param[$i][$j]], $this->acciones[$i]));
                        }
                    } else {
                        print(str_replace('%' . $this->param[$i] . '%', $row[$this->param[$i]], $this->acciones[$i]));
                    }
                }
                print("</td>");
            }
            //
            print("</tr>");
        }
    }

    function create2() {

        print("<table cellpadding=10 cellspacing=10 width=$this->width border=0 class=tablaDatos>");

        $this->header();
        $this->content();
        //

        print("</table>");
    }
    
    function formatLabel($format, $var) {
        switch ($format)
        {
            case "Peso":
                return "$" . number_format($var,0,",",".");
                break;
            case "Date":
                list($anio, $mes, $dia) = explode("-", $var);
                return $dia."-".$mes."-".$anio;
                break;
            default : 
                return $var;
        }
    }

}

//
?>